package com.sjh.lib_app.qrCode

import android.Manifest
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.widget.LinearLayout
import androidx.appcompat.app.AlertDialog
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.PermissionUtils
import com.sjh.baseutils.toast
import com.sjh.baseutils.zxing.android.CaptureActivity
import com.sjh.lib_app.baseui.BaseActivity
import com.sjh.lib_app.R
import com.sjh.lib_app.event.QrCodeResult
import org.greenrobot.eventbus.EventBus
import pub.devrel.easypermissions.EasyPermissions
import pub.devrel.easypermissions.PermissionRequest

@Route(path = "/app/qrCode", name = "二维码扫描页面")
class QrCodeActivity1 : BaseActivity() {
    override fun onCreateRootView() = LinearLayout(this)

    override fun onInitView(savedInstanceState: Bundle?) {

        if (EasyPermissions.hasPermissions(this, Manifest.permission.CAMERA)) {
            startQrCode()
        } else {
            val permissionRequest = PermissionRequest.Builder(
                this, 121, *arrayOf(
                    Manifest.permission.CAMERA,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION
                )
            )
                .setRationale("为了使用二维码，我们需要相机权限")
                .setTheme(R.style.Theme_AppCompat_Light_Dialog).build()
            EasyPermissions.requestPermissions(permissionRequest)
        }
    }

    private fun startQrCode() {
        startActivityForResult(Intent(this, CaptureActivity::class.java), 102)
    }

    override fun initListener() {
    }

    override fun initData() {
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode != 102 || data == null) {
            "扫码失败,请重试".toast()
            finish()
            return
        }
        val stringExtra = data.getStringExtra("codedContent")
        EventBus.getDefault().post(QrCodeResult(stringExtra ?: ""))
        finish()
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        var isGranted = true
        for (granted in grantResults) {
            if (granted == -1) {
                isGranted = false
                break
            }
        }
        if (isGranted) {
            startQrCode()
        } else {
            val alertDialog = AlertDialog.Builder(
                this@QrCodeActivity1,
                R.style.Theme_AppCompat_Light_Dialog
            ).setCancelable(false)
                .setTitle("权限申请提示")
                .setMessage("打开扫码功能需要相机权限，请打开相应权")
                .setNegativeButton("不同意") { dialogInterface: DialogInterface, i: Int ->
                    finish()
                }
                .setPositiveButton("同意") { dialogInterface: DialogInterface, i: Int ->
                    finish()
                    PermissionUtils.launchAppDetailsSettings()
                }.create()
            alertDialog.setCancelable(false)
            alertDialog.show()
        }
    }

}